package io.github.wanggit.antrpc.console.zookeeper;

import io.github.wanggit.antrpc.commons.bean.InterfaceNodeDataBean;
import io.github.wanggit.antrpc.commons.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DefaultInterfaceContainer implements IInterfaceContainer {

    private final Set<InterfaceNodeDataBean> interfaceNodeDataBeans = new CopyOnWriteArraySet<>();

    @Override
    public void addInterface(InterfaceNodeDataBean interfaceNodeDataBean) {
        if (log.isInfoEnabled()) {
            log.info("save to local --> " + JsonUtils.toJsonPrettyString(interfaceNodeDataBean));
        }
        interfaceNodeDataBeans.add(interfaceNodeDataBean);
    }

    @Override
    public void updateInterface(InterfaceNodeDataBean interfaceNodeDataBean) {
        if (log.isDebugEnabled()) {
            log.debug("update " + interfaceNodeDataBean.getFullPath());
        }
        deleteInterface(interfaceNodeDataBean);
        addInterface(interfaceNodeDataBean);
    }

    @Override
    public void deleteInterface(InterfaceNodeDataBean interfaceNodeDataBean) {
        if (log.isDebugEnabled()) {
            log.debug("delete " + interfaceNodeDataBean.getFullPath());
        }
        List<InterfaceNodeDataBean> deleteTargets =
                interfaceNodeDataBeans.stream()
                        .filter(
                                it ->
                                        Objects.equals(
                                                it.getFullPath(),
                                                interfaceNodeDataBean.getFullPath()))
                        .collect(Collectors.toList());
        interfaceNodeDataBeans.removeAll(deleteTargets);
    }

    @Override
    public List<InterfaceDTO> findContainsByInterfaceName(String interfaceName) {
        return interfaceNodeDataBeans.stream()
                .filter(
                        it ->
                                null != it.getClassName()
                                        && it.getClassName()
                                                .toLowerCase()
                                                .contains(interfaceName.toLowerCase()))
                .sorted(Comparator.comparing(InterfaceNodeDataBean::getClassName))
                .map(InterfaceDTO::fromInterfaceNodeDataBean)
                .collect(Collectors.toList());
    }

    @Override
    public List<InterfaceDTO> findByInterfaceName(String className) {
        return interfaceNodeDataBeans.stream()
                .filter(
                        it ->
                                null != it.getClassName()
                                        && Objects.equals(it.getClassName(), className))
                .sorted(Comparator.comparing(InterfaceNodeDataBean::getHostInfo))
                .map(InterfaceDTO::fromInterfaceNodeDataBean)
                .collect(Collectors.toList());
    }

    @Override
    public InterfaceDTO findByInterfaceNameAndHostInfo(String className, String hostInfo) {
        List<InterfaceDTO> interfaceDTOS =
                interfaceNodeDataBeans.stream()
                        .filter(
                                it ->
                                        Objects.equals(className, it.getClassName())
                                                && Objects.equals(hostInfo, it.getHostInfo()))
                        .map(InterfaceDTO::fromInterfaceNodeDataBean)
                        .collect(Collectors.toList());
        return interfaceDTOS.isEmpty() ? null : interfaceDTOS.get(0);
    }
}
